package com.zyg.zlib;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class MainActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        RecyclerView recycler = findViewById(R.id.recycler);
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        recycler.addItemDecoration(new DividerItemDecoration(mSelf, LinearLayout.VERTICAL));
        recycler.setLayoutManager(layoutManager);
        SimpleListAdapter mAdapter = new SimpleListAdapter(items());
        recycler.setAdapter(mAdapter);
    }

    private List<SimpleItem> items() {
        List<SimpleItem> list = new ArrayList<>();
        list.add(new SimpleItem("LableDrawable", LableDrawableActivity.class));
        list.add(new SimpleItem("LableDrawable", null));
        return list;
    }

    class SimpleListAdapter extends RecyclerView.Adapter<SimpleListAdapter.ViewHolder> {

        private List<SimpleItem> items;

        SimpleListAdapter(List<SimpleItem> items) {
            if (items == null) {
                this.items = Collections.emptyList();
            }
            this.items = items;
        }

        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.simple_item_layout, parent, false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            final SimpleItem item = items.get(position);
            holder.textView.setText(item.desc);
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mSelf.startActivity(new Intent(mSelf, item.clazz));
                }
            });
        }

        @Override
        public int getItemCount() {
            return items.size();
        }

        class ViewHolder extends RecyclerView.ViewHolder {

            TextView textView;

            ViewHolder(@NonNull View itemView) {
                super(itemView);
                this.textView = itemView.findViewById(R.id.tv_name);
            }
        }
    }

    static class SimpleItem {
        String desc;
        Class clazz;

        SimpleItem(String desc, Class clazz) {
            this.desc = desc;
            this.clazz = clazz;
        }
    }
}
